#!/usr/bin/bash

set -e

add_to_steam() {
    encodedUrl="steam://addnonsteamgame/$(python3 -c "import urllib.parse;print(urllib.parse.quote(\"$1\", safe=''))")"
    touch /tmp/addnonsteamgamefile
    steam "$encodedUrl"
}

show_error() {
    if [ "$show_dialog" = "1" ]; then
        zenity --title Error --error --text "$1"
    else
        echo "$1" >&2
    fi
}

if [ $(id -u) = "0" ]; then
    show_error "This script cannot be run as root"
    exit 1
fi

if [ "$XDG_SESSION_TYPE" = "tty" ] && ! pgrep -x steam >/dev/null 2>&1; then
   show_error "Cannot run this script from a tty if Steam is not running"
   exit 1
fi

if [ "$1" = "-ui" ]; then
    show_dialog=1
    shift
fi

file=$(realpath "$1")
if [ ! -e "$file" ]
then
    echo "Usage: steamos-add-to-steam [-ui] <path>"
    exit 1
fi
mime=$(xdg-mime query filetype "$file")
case "$mime" in
    "application/x-desktop"|"application/x-ms-dos-executable")
        add_to_steam "$file"
        ;;
    "application/x-executable"|"application/vnd.appimage"|"application/x-shellscript")
        if [ -x "$file" ]; then
            add_to_steam "$file"
        else
            show_error "Unable to add non-Steam game. Is the file executable?"
        fi
        ;;
    *)
        show_error "Unsupported file type"
        ;;
esac
